<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="900px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="135px">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="计划名称">
            <el-input v-model="form.planName" style="width: 200px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="巡检对象" prop="objName">
            <el-select
              v-model="form.objId"
              placeholder="巡检对象"
              multiple
              filterable
              style="width: 400px"
              @change="eqptSelect($event)"
            >
              <el-option
                v-for="(itemobject,index) in inspectionObject"
                :key="index"
                :label="itemobject.eqptcode+'/'+itemobject.eqptname"
                :value="itemobject.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="巡检模板名称">
            <el-input v-model="form.templateName" style="width: 200px;" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="巡检计划说明">
            <el-input v-model="form.planDesc" style="width: 200px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="巡检周期" prop="periodName">
            <el-select
              v-model="form.periodId"
              placeholder="巡检周期"
              style="width:200px;"
              @keydown.native="myMoneyKeyDown"
              @keyup.enter.native="onSubmitPeriod"
              @change="periodSelect($event)"
            >
              <el-option v-for="(itemobject,index) in periodItem" :key="index" :label="itemobject.name" :value="itemobject.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="巡检人名称">
            <el-input v-model="form.inspectorName" style="width: 200px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="开始时间">
            <el-date-picker
              v-model="form.startTime"
              style="width: 200px"
              type="date"
              placeholder="请选择"
              :format="'yyyy-MM-dd'"
              :picker-options="startTimeConfig"
              @change="startTimeChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间">
            <el-date-picker
              v-model="form.endTime"
              style="width: 200px"
              type="date"
              placeholder="请选择"
              :format="'yyyy-MM-dd'"
              :picker-options="endTimeConfig"
              @change="endTimeChange"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="巡检时间">
            <el-date-picker
              v-model="form.operateTime"
              style="width: 200px"
              type="date"
              placeholder="请选择"
              :format="'yyyy-MM-dd'"
              :picker-options="operateTimeConfig"
              @change="operateTimeChange"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/tInspectionTask'
import { validateNumber } from '@/utils/validate'
import { queryEqpt } from '@/api/tEquipment'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    supThis: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      title: '',
      periodItem: [
        { id: 1, name: '年' },
        { id: 2, name: '半年' },
        { id: 3, name: '季' },
        { id: 4, name: '月' },
        { id: 5, name: '周' }
      ],
      dicts: [{ id: 1, value: '1', label: '启动' }, { id: 2, value: '0', label: '暂定' }],
      inspectionObject: [],
      realTimeInfo: {
        startTime: '',
        endTime: ''
      },
      startTimeConfig: {},
      endTimeConfig: {},
      form: {
        id: '',
        planName: '',
        objId: '',
        objName: '',
        templateId: '',
        templateName: '',
        planDesc: '',
        periodId: '',
        periodName: '',
        inspectorId: '',
        inspectorName: '',
        vehTypeId: '',
        vehTypeName: '',
        startTime: '',
        endTime: '',
        operateTime: '',
        state: ''
      },
      rules: {
      }
    }
  },
  mounted: {

  },
  methods: {
    cancel() {
      this.resetForm()
    },
    myMoneyKeyDown(event) {
      validateNumber(event)
    },
    periodSelect(event) {
      for (var index in this.periodItem) {
        if (this.periodItem[index].id === event) {
          this.form.periodName = this.periodItem[index].name
        }
      }
    },
    onSubmitPeriod(event) {
      const _this = this
      let flag = false
      const label = event.target.value + '天'
      this.periodItem.forEach(item => {
        if (item.id === -1) {
          item.name = label
          flag = true
        }
      })
      if (!flag) {
        _this.periodItem.push({ id: -1, name: label })
      }
      _this.form.periodId = -1
      _this.form.periodName = label
      event.target.blur()
    },
    eqptSelect(event) {
      this.form.objName = event.toString()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.supThis.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    queryAllEqpt() {
      queryEqpt({ size: 9999, pageNo: 0 }).then(res => {
        this.inspectionObject = res.content
      })
    },
    startTimeChange(val) {
      this.realTimeInfo.startTime = val
    },
    endTimeChange(val) {
      this.realTimeInfo.endTime = val
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        planName: '',
        objId: '',
        objName: '',
        templateId: '',
        templateName: '',
        planDesc: '',
        periodId: '',
        periodName: '',
        inspectorId: '',
        inspectorName: '',
        vehTypeId: '',
        vehTypeName: '',
        startTime: '',
        endTime: '',
        operateTime: '',
        state: ''
      }
    }
  }
}
</script>

<style scoped>
.input_text {
  width: 300px;
}
</style>
